//
//  CareOrgViewController.swift
//  LongTermInsurance
//
//  Created by zolobdz on 2018/11/24.
//  Copyright © 2018年 HeChao. All rights reserved.
//

import UIKit
import ObjectMapper
import MJRefresh

class CareOrgViewController: BaseTableViewController {
    
    var notifyArray = [CareOrgResultModel]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "护理机构"
        view.backgroundColor = .white
        // Do any additional setup after loading the view.
        tableView.backgroundColor = CDColor.grayBackgroundColorA
        tableView.register(ORGCareResultCell.self, forCellReuseIdentifier: "ORGCareResultCell")
        tableView.separatorColor = .clear
        tableView.estimatedRowHeight = 0
        tableView.rowHeight = 220
        
        header.isHidden = false
        
        tableView.reloadData()
        header.beginRefreshing()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
    
    override func pullRefresh() {
        getNotifyData()
    }
    
    
    func getNotifyData() {
        guard let info = MyselfSingleton.shared.userInfo else{
            return
        }
        let json = ["PersionID": info.userID].convertToJsonString()
        let paramString = "<json>"+json+"</json>"
        NetworkingHandle.fetchNetworkData(method: "getNurseInfoOrganization", paramString: paramString, success: {[weak self] (dict) in
            self?.header.endRefreshing()
            if let imgsString = dict["Detail"] as? String {
                if let array = Mapper<CareOrgResultModel>().mapArray(JSONString: imgsString) {
                    self?.notifyArray = array
                    self?.tableView.reloadData()
                }
            }
            if let code = dict["ResultCode"] as? Int,code == 1 {
                if let msgString = dict["ResultMessage"] as? String {
                    CDUtility.showMSG(message: msgString)
                }
            }
        }) { [weak self] in
            self?.header.endRefreshing()
            print("")
        }
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return notifyArray.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ORGCareResultCell", for: indexPath) as! ORGCareResultCell
        cell.config(model: notifyArray[indexPath.row])
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    }
}
